//
//  HJStatusListViewModel.swift
//  WeiBo
//
//  Created by 韩俊 on 15/12/9.
//  Copyright © 2015年 HanJun. All rights reserved.
//

import UIKit





class HJStatusListViewModel: NSObject {
    
    var statues: [HJStatusViewModel]?

    func loadData(isPullUp: Bool, complete:(isSuccess:Bool, count: Int)->()){
        
        var maxId = isPullUp == true ? (statues?.last?.status?.id ?? 0) : 0
        
        if maxId > 0 {
            
            maxId -= 1
        }
        let sinceId = isPullUp == true ? 0 : (statues?.first?.status?.id ?? 0)
        
        HJNetworkTools.shareTools.loadStatus(HJUserAccountViewModel.sharedAccount.accessToken!,sinceId: sinceId , maxid: maxId ) { (response, error) -> () in
            if error != nil {
                
                print("加载失败\(error)")
                return
            }
            
            guard let result = response as? [String: AnyObject] else {
                print("返回的数据不是一个字典")
                return
            }
            
            
            //print(response)
            guard let dic = result["statuses"] as? [[String:AnyObject]] else{
                
                print("没有statuses")
                return
            }
            
            var tempArray = [HJStatusViewModel]()
            for statusDic in dic {
                
                let status = HJStatus(dic: statusDic)
                tempArray.append(HJStatusViewModel(status: status))
            }
            
            if self.statues == nil {
                self.statues = [HJStatusViewModel]()
            }
            if isPullUp {
                 self.statues = self.statues! + tempArray
            }else{
                 self.statues = tempArray + self.statues!
            }
           
            complete(isSuccess: true,count: tempArray.count)

        }
   }
}